package src.chat;

import java.util.ArrayList;

import src.User;

public class ChatRoom {

	private String chatRoomName;
	private ArrayList<User> roomMembers;
	private ArrayList<ChatMessage> roomMessages;
	private ArrayList<PrivateChatMessage> roomPrivateMessages;
	private int    max_msgs, max_users, max_pvt_msgs, max_latent_time;

	public ChatRoom(String _name,int _max_msgs,	int _max_users,	int _max_pvt_msgs,	int _max_latent_time){
		chatRoomName            = _name;
		max_msgs        		= _max_msgs;
		max_users       		= _max_users;
		max_pvt_msgs    		= _max_pvt_msgs;
		max_latent_time 		= _max_latent_time;
		roomMembers           	= new ArrayList();
		roomMessages        	= new ArrayList();
		roomPrivateMessages    	= new ArrayList();
	}

	public synchronized void addMessage(ChatMessage aMessage){
		refreshUsers();
		roomMessages.add(aMessage);
	}

	public synchronized void addPrivateMessage(PrivateChatMessage aMessage){
		roomPrivateMessages.add(aMessage);
	}

	public synchronized ArrayList getMessages(){
		refreshUsers();
		return roomMessages;
	}

	public synchronized ArrayList getAllPrivateMessages(){
		return roomPrivateMessages;
	}

	public synchronized ArrayList getPrivateMessages(User user){
		refreshUsers();
		return roomPrivateMessages;
	}

	public synchronized int removePrivateMessages(User user){
		int removed = 0;
		for(PrivateChatMessage p : roomPrivateMessages)
		{
			if(user.getIdUser() == p.getRecipient().getIdUser())
			{
				roomPrivateMessages.remove(p);
				removed++;
			}
		}

		return removed;
	}

	public synchronized void removeAllPrivateMessages(){
		roomPrivateMessages.removeAll(roomPrivateMessages);
	}

	public synchronized void addUser(User aUser)throws Exception{
		roomMembers.add(aUser);
	}

	public synchronized void removeUser(User aUser){
		roomMembers.remove(aUser);
	}

	public synchronized ArrayList getUsers(){
		refreshUsers();
		return roomMembers;
	}

	public void refreshUsers(){
		for(User user : roomMembers)
		{
			if (!user.refresh()) {
				removeUser(user);
				addMessage(new ChatMessage(new User(0,"JCHAT_SERVLET", "", "EU", "0.0.0.0"),"REMOVED FOR INACTIVITY"));
			}
		}
	}


	/**
	 *   @return int - the number of users logged into this channel
	 **/
	public synchronized int size(){
		return roomMembers.size();
	}

	/**
	   <pre>
	   getUser
	   @param int - a user id
	   @return org.ansurgen.jchat.User
	   </pre>
	 */
	public synchronized User getUser(int uid) throws Exception{
		User usr = null;
		for(User user : roomMembers){
			if (user.getIdUser() == uid) {
				//we found the user...
				usr = user;
				break;
			}else{
				usr = null;
			}
		}

		if (usr == null) {
			throw new Exception("User not found [" + uid + "]");
		}
		return usr;
	}

	public String getChatRoomName() {
		return chatRoomName;
	}

	public void setChatRoomName(String chatRoomName) {
		this.chatRoomName = chatRoomName;
	}

	public ArrayList<User> getRoomMembers() {
		return roomMembers;
	}

	public void setRoomMembers(ArrayList<User> roomMembers) {
		this.roomMembers = roomMembers;
	}

	public ArrayList<ChatMessage> getRoomMessages() {
		return roomMessages;
	}

	public void setRoomMessages(ArrayList<ChatMessage> roomMessages) {
		this.roomMessages = roomMessages;
	}

	public ArrayList<PrivateChatMessage> getRoomPrivateMessages() {
		return roomPrivateMessages;
	}

	public void setRoomPrivateMessages(
			ArrayList<PrivateChatMessage> roomPrivateMessages) {
		this.roomPrivateMessages = roomPrivateMessages;
	}

	public int getMax_msgs() {
		return max_msgs;
	}

	public void setMax_msgs(int maxMsgs) {
		max_msgs = maxMsgs;
	}

	public int getMax_users() {
		return max_users;
	}

	public void setMax_users(int maxUsers) {
		max_users = maxUsers;
	}

	public int getMax_pvt_msgs() {
		return max_pvt_msgs;
	}

	public void setMax_pvt_msgs(int maxPvtMsgs) {
		max_pvt_msgs = maxPvtMsgs;
	}

	public int getMax_latent_time() {
		return max_latent_time;
	}

	public void setMax_latent_time(int maxLatentTime) {
		max_latent_time = maxLatentTime;
	}
}
